package com.hnkywl.sts.dao.kcgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.cggl.Glfy;
import com.hnkywl.sts.entity.xsgl.Zkd;
import com.hnkywl.sts.entity.xsgl.Zkdmx;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.*;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-15
 * Time: 下午2:14
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class ZkdDao extends SimpleHibernateTemplate<Zkd, Long> {
    public Page<Zkd> findZkdList(String zkdh, int pageNo, int pageSize) {
        Page<Zkd> page = new Page<Zkd>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("dateCreated");
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("sqgs", "sqgs", CriteriaSpecification.LEFT_JOIN)
                .createAlias("zcck", "zcck", CriteriaSpecification.LEFT_JOIN)
                .createAlias("zrck", "zrck", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Projections.sqlProjection("zkdh as zkdh", new String[]{"zkdh"}, new Type[]{Hibernate.STRING}))
                        .add(Property.forName("sqgs.id").as("sqgs_id"))
                        .add(Property.forName("sqgs.jc").as("sqgs_jc"))
                        .add(Property.forName("zcck.id").as("zcck_id"))
                        .add(Property.forName("zcck.mc").as("zcck_mc"))
                        .add(Property.forName("zrck.id").as("zrck_id"))
                        .add(Property.forName("zrck.mc").as("zrck_mc"))
                        .add(Property.forName("type").as("type"))
                        .add(Property.forName("xyps").as("xyps"))
                        .add(Property.forName("yps").as("yps"))
                        .add(Projections.sqlProjection("zkrq as zkrq", new String[]{"zkrq"}, new Type[]{Hibernate.DATE}))
                        .add(Projections.sqlProjection("zrrq as zrrq", new String[]{"zrrq"}, new Type[]{Hibernate.DATE}))
                        .add(Projections.sqlProjection("this_.zt as zt", new String[]{"zt"}, new Type[]{Hibernate.STRING}))
                        .add(Property.forName("lastUpdated"))
                )
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);

        if (StringUtils.isNotEmpty(zkdh)) {
            criteria.add(Property.forName("zkdh").like(zkdh, MatchMode.START));
        }
        RegionDao.createDataRegion(criteria);
        Page list = this.findByCriteria(page, criteria);
        return list;
    }

    public List<Map> findZkdmxList(Long zkdId) {
        Criteria criteria = getSession().createCriteria(Zkdmx.class)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("zkd", "zkd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("newkh", "newkh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.jtwz", "wzgg", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("kh.id").as("kh.id"))
                        .add(Property.forName("kh.khbm").as("kh.khbm"))
                        .add(Property.forName("zkd.zkdh").as("zkd.zkdh"))
                        .add(Property.forName("newkh.khbm").as("kh.nkhbm"))
                        .add(Property.forName("wzgg.id").as("wzgg.id"))
                        .add(Property.forName("wzgg.code").as("wzgg.code"))
                        .add(Property.forName("wzgg.name").as("wzgg.name"))
                        .add(Property.forName("wzgg.ggxh").as("wzgg.ggxh"))
                        .add(Property.forName("wzgg.dw").as("wzgg.dw"))
                        .add(Property.forName("wzgg.th").as("wzgg.th"))
                        .add(Property.forName("zkjs").as("zkjs"))
                        .add(Property.forName("zksl").as("zksl"))
                        .add(Property.forName("zcjs").as("zcjs"))
                        .add(Property.forName("zcsl").as("zcsl"))
                        .add(Property.forName("zrjs").as("zrjs"))
                        .add(Property.forName("zrsl").as("zrsl"))
                        .add(Property.forName("bz").as("bz"))
                ).add(Restrictions.eq("zkd.id", zkdId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return (List<Map>) criteria.list();
    }

    public List<Map> findSskcZkdmxList(Long khId) {
        Criteria criteria = getSession().createCriteria(Zkdmx.class)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("zkd", "zkd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("newkh", "newkh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.jtwz", "wzgg", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Projections.sqlProjection("kh1_.id as kh_id", new String[]{"kh_id"}, new Type[]{Hibernate.LONG}))
                        .add(Property.forName("kh.khbm").as("kh.khbm"))
                        .add(Property.forName("zkd.zkdh").as("zkd.zkdh"))
                        .add(Property.forName("newkh.khbm").as("kh.nkhbm"))
                        .add(Property.forName("wzgg.id").as("wzgg.id"))
                        .add(Property.forName("wzgg.code").as("wzgg.code"))
                        .add(Property.forName("wzgg.name").as("wzgg.name"))
                        .add(Property.forName("wzgg.ggxh").as("wzgg.ggxh"))
                        .add(Property.forName("wzgg.dw").as("wzgg.dw"))
                        .add(Property.forName("wzgg.th").as("wzgg.th"))
                        .add(Property.forName("zkjs").as("zkjs"))
                        .add(Property.forName("zksl").as("zksl"))
                        .add(Property.forName("zcjs").as("zcjs"))
                        .add(Property.forName("zcsl").as("zcsl"))
                        .add(Property.forName("zrjs").as("zrjs"))
                        .add(Property.forName("zrsl").as("zrsl"))
                        .add(Property.forName("bz").as("bz"))
                ).add(Restrictions.eq("kh.id", khId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return (List<Map>) criteria.list();
    }

    public List<Map> findZkfyList(Long zkdId) {
        Criteria criteria = getSession().createCriteria(Glfy.class)
                .createAlias("sydw", "sydw", CriteriaSpecification.LEFT_JOIN)
                .createAlias("fylb", "fylb", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("sydw.id").as("sydw.id"))
                        .add(Property.forName("sydw.qc").as("sydw.qc"))
                        .add(Property.forName("fylb.id").as("fylb.id"))
                        .add(Property.forName("fylb.fykmjc").as("fylb.fykmjc"))
                        .add(Property.forName("jfsl").as("jfsl"))
                        .add(Property.forName("dj").as("dj"))
                        .add(Property.forName("je").as("je"))
                        .add(Property.forName("bz").as("bz"))
                ).add(Restrictions.eq("zkd.id", zkdId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return (List<Map>) criteria.list();
    }

    public boolean existZkd(Long bgjhhzmxId) {
        String sql = " select count(1) from t_biz_zkd z left join t_biz_bgjhhzmx bgjhhzmx on z.bgjhhzmx_id = bgjhhzmx.id where bgjhhzmx.id = :bgjhhzmxId ";
        SQLQuery sqlQuery = getSession().createSQLQuery(sql);
        sqlQuery.setLong("bgjhhzmxId", bgjhhzmxId);
        return Integer.parseInt(sqlQuery.uniqueResult().toString()) != 0;
    }

    public List<Zkd> findZkdByBgjhhzmxId(Long id) {
        String hql = "from Zkd where bgjhhzmx.id=?";
        return createQuery(hql, id).list();
    }
}


